package com.yjzx.util.common.util.exception;

import java.util.Objects;

/**
 * @author guozhi
 * @date 2024/9/6
 * @description FunctionThrow
 */
@FunctionalInterface
public interface FunctionThrow<T, R, E extends Throwable> {

    R apply(T t) throws E;

    default <V> FunctionThrow<V, R, E> compose(FunctionThrow<? super V, ? extends T, E> before) {
        Objects.requireNonNull(before);
        return (V v) -> apply(before.apply(v));
    }

    default <V> FunctionThrow<T, V, E> andThen(FunctionThrow<? super R, ? extends V, E> after) {
        Objects.requireNonNull(after);
        return (T t) -> after.apply(apply(t));
    }

    static <T,E extends Throwable> FunctionThrow<T, T,E> identity() {
        return t -> t;
    }
}
